from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from blog.models import Post
from common.easyfeed import EasyFeed
from settings import MEDIA_ROOT, MEDIA_URL
import os

current_site = Site.objects.get_current().name

class LatestPostFeed(EasyFeed):
  '''
    Generates latest 50 objects from models.Post
  '''

  def __init__(self, feed_url, req, **kwargs):
    super(LatestPostFeed, self).__init__(feed_url)
    self.feed_url = feed_url
    self.request = req

    if kwargs:
      self.kwargs = kwargs

  def link(self):
    return '/b/'

  def title(self):
    return '%s :: latest posts' % current_site

  def description(self):
    return 'random ravings'

  def items(self):
    return Post.objects.order_by('-pub_date')[:50]

  def item_link(self, item):
    return item.get_absolute_url()

  def item_description(self, item):
    return item.title

  def item_title(self, item):
    return item.title

  def item_pubdate(self, item):
    return item.pub_date

  def item_author_name(self, item):
    return item.user.username

  def item_enclosure_url(self, item):
    return 'http://%s%s%s' % (self.request.META['HTTP_HOST'],MEDIA_URL, item.enclosure)

  def item_enclosure_length(self, item):
    return os.stat('%s/%s'% (MEDIA_ROOT, item.enclosure)).st_size

  def item_enclosure_mime_type(self, item):
    #FIXME: make this dynamic
    return 'audio/mpeg'
